package cn.wbu.zyh.controller.user;

import cn.wbu.zyh.dto.TeamDTO;
import cn.wbu.zyh.dto.TeamMemberDTO;
import cn.wbu.zyh.dto.TeamMemberPageQueryDTO;
import cn.wbu.zyh.result.PageResult;
import cn.wbu.zyh.result.Result;
import cn.wbu.zyh.service.TeamService;
import cn.wbu.zyh.vo.TeamVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/team")
@Slf4j
public class TeamController {
    @Autowired
    private TeamService teamService;
    @PostMapping("/create")
    @ApiOperation("创建团队")
    public Result<TeamDTO> createTeam(@RequestBody TeamDTO teamDTO) {
        teamService.createTeam(teamDTO);
        log.info("创建团队:{}",teamDTO);
        return Result.success();
    }
    @PostMapping
    public Result<TeamVO> getHasTeamAndInfo(@RequestBody String username) {
        TeamVO teamVO = teamService.getHasTeam(username);
        log.info("hasteam:{}",teamVO);
        return Result.success(teamVO);
    }

    @PostMapping("/leave")
    public Result leaveTeam(@RequestBody TeamDTO teamDTO) {
        teamService.leaveTeam(teamDTO);
        return Result.success();
    }

    @PostMapping("/checkTLQuitTeam")
    public Result CheckTLQuitTeam(@RequestBody TeamDTO teamDTO){
        teamService.CheckTLQuitTeam(teamDTO);
        return Result.success();
    }
    //这段逻辑可以优化下：先判断是团队创建人再解散
    @DeleteMapping("/dismiss")
    public Result dismissTeam(@RequestParam String teamId){
        teamService.deleteTeam(teamId);
        return Result.success();
    }
    @GetMapping("/list")
    @ApiOperation("团队人员")
    public Result<PageResult> page(TeamMemberPageQueryDTO teamMemberPageQueryDTO){
        log.info("员工分页查询,参数为：:{}",teamMemberPageQueryDTO);
        PageResult pageResult = teamService.pageQuery(teamMemberPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @ApiOperation("移除成员")
    public Result delete(@RequestParam String username){
        teamService.RemoveTeamMember(username);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改权限")
    public Result update(@RequestBody TeamMemberDTO teamMemberDTO) {
        teamService.update(teamMemberDTO);
        return Result.success();
    }
}
